<?php
class Customer_OrdersController extends Zend_Controller_Action
{
	protected $_session;
	
    private function _getCustomerNS()
    {
		if($this->_session == null) {
			$this->_session = new Zend_Session_Namespace('Customer');
		}
		return $this->_session;
    }
    
	public function reviewAction()
	{
		// get params from session		
		$tripID = $this->_getCustomerNS()->tripID;
		$seatCountYouWant = $this->_getCustomerNS()->seatCountYouWant;
		$selectedSeatNumbers = $this->_getCustomerNS()->selectedSeatNumbers;		
		
		// get trip info
		$tripModel = new Customer_Model_Trip();
		$trip = $tripModel->getTripInfo($tripID);			
		if($trip == null) {
			throw new Exception('You can\'t reach here');
		}
		
		// prepare for "review" view
		$this->view->from_city = $trip['from_city'];
		$this->view->to_city = $trip['to_city'];
		$this->view->departure_time = $trip['departure_time'];
		$this->view->boarding_point = $trip['boarding_point'];
		$this->view->fare = $trip['fare'];		
		$this->view->seatCountYouWant = $seatCountYouWant;
		$this->view->selectedSeatNumbers = $selectedSeatNumbers;	
		
		// create captcha form
		$bookingFinishForm = new Customer_Form_BookingFinish();
		$bookingFinishForm->setAction('/orders/review')->setMethod('post');
		$fromCustomerEnterInfo = $this->_request->getParam('fromCustomerEnterInfo');
		
		// check if previous page is customer/enter-info		
		if($fromCustomerEnterInfo) {
			// empty
		} else {			
			if($this->_request->isPost() && $bookingFinishForm->isValid($_POST)) {				
				return $this->_forward('create', 'orders', 'customer');								
			}
		}
		$this->view->bookingFinishForm = $bookingFinishForm;
	}
    
    public function createAction()
    {
		TBB_Utility_Redirector::redirectIfSessionExpired();		
    	
		// get params from session
    	$customerID = $this->_getCustomerNS()->customerID;
    	$tripID = $this->_getCustomerNS()->tripID;
    	$seatCountYouWant = $this->_getCustomerNS()->seatCountYouWant;
    	$seats = $this->_getCustomerNS()->seats;
    	$tempSeats = $this->_getCustomerNS()->tempSeats;    	
    	
		$bookingModel = new Customer_Model_Booking();
		if(($identity = TBB_Utility_Checker::isLoggedInUser()) && 
		   ($customerID = TBB_Utility_Checker::isCustomer($identity))) {
			try {
				// do the booking for the user has customer role
				$code = $bookingModel->bookingForExistCustomer($customerID, 
				 											   $tripID, 
				 											   $seatCountYouWant, 
				 											   $seats, 
				 											   $tempSeats);
			} catch(Exception $e) {
				return $this->_redirect('/error/expire');
			}
		} else {
			try {
				$data = $this->_getCustomerNS()->customerData;
				$code = $bookingModel->booking($data['name'],
										$data['email'],
										$data['ssn'],
										$data['mobile'],
										$tripID,
										$seatCountYouWant,
										$seats,
										$tempSeats);
			} catch(Exception $e) {
				return $this->_redirect('/error/expire');
			}
		}
		
		// store order code
		$this->_getCustomerNS()->code = $code;
		// mark this session as expired
		TBB_Utility_Redirector::setExpiredForSession(true);
		// receive the order, end the booking process.		
		return $this->_redirect('/payment/receive-code');	
    }
    
	public function cancelAction()
	{
		$ordersForm = new Customer_Form_Orders();
		$ordersForm->setAction('/orders/cancel/')->setMethod('post');
		
		if($this->_request->isPost()) {			
			if($ordersForm->isValid($_POST))
			{		
				$code = $ordersForm->getValue('order');
				$bookingModel = new Customer_Model_Booking();
				try {
					$bookingModel->cancelOrder($code);
					return TBB_Utility_Redirector::redirect('Bạn đã hủy vé thành công.', 'customer', 'orders', 'after-cancel');	
				} catch(Exception $e) {
					return TBB_Utility_Redirector::redirect($e->getMessage(), 'customer', 'orders', 'after-cancel');
				}											
			} else {
				$this->view->error = true;
			}
		}
		$this->view->form = $ordersForm;
	}
	
	public function afterCancelAction()
	{
		$this->message = TBB_Utility_Redirector::getMessage();
	}
	
	public function indexAction()
	{
		
	}
}